package utils.arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import utils.bean.ObjectUtils;
import utils.test.Assert;

/**
 * 数组工具
 * @author 曹炳俊
 *
 */
public class ArraysUtils {
	/**
	 * 将原始数组后面追加数组返回新数组
	 * @param <T>
	 * @param original 原始数组
	 * @param appendArray 追加数组
	 * @return 返回追加后数组
	 */
	public static <T> T[] append(T[] originalArray,final T ... appendArray){
		Assert.isNotNull(originalArray, "Parameter isn't allow null, original is null");
		Assert.isNotNull(originalArray, "Parameter isn't allow null, appendArray is null");
		if (appendArray == null || appendArray.length == 0) {
			return originalArray;
		}
		int oldLength = originalArray.length;
		int newLength = originalArray.length + appendArray.length;
		originalArray = Arrays.copyOf(originalArray, newLength);
		for (int i = oldLength,j=0; i < newLength; i++,j++) {
			Arrays.fill(originalArray, i,i+1,appendArray[j]);
		}
		return originalArray;
	}
	
	/**
	 * 合并多个数组
	 * @param lists 传合并的数组
	 * @return
	 */
	public static <T> List<T> merge(List<T> ... lists){
		if (ObjectUtils.hasNotLength(lists)) {
			return null;
		}
		List<T> allList = new ArrayList<T>();
		for (List list : lists) {
			allList.addAll(list);
		}
		return allList;
	}
}
